﻿//-----------------------------------------------------------------------
// <copyright file="MegabyteLabel.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.CustomControls
{
    using System.ComponentModel;
    using System.Drawing;

    /// <summary>A custom label based on the BoldLabel class for showing formatted megabyte values.</summary>
    public class MegabyteLabel : BoldLabel
    {
        /// <summary>The current megabyte value for the label.</summary>
        private int value;

        /// <summary>Indicates whether the label is currently in 'Reserve' mode which shows the value as a positive or negatve value.</summary>
        private bool reserveMode;

        /// <summary>A System.Drawing.Color representing the default forecolor of the label.</summary>
        private Color defaultColor;

        /// <summary>Initializes a new instance of the MegabyteLabel class.</summary>
        public MegabyteLabel()
        {
            this.defaultColor = this.ForeColor;
            base.Text = "0 MB";
        }

        /// <summary>Gets or sets a value indicating whether the label should show megabytes as a positive or negative value.</summary>
        [Browsable(true)]
        public bool ReserveMode
        {
            get
            {
                return this.reserveMode;
            }

            set
            {
                this.reserveMode = value;
                this.Value = this.value;
            }
        }

        /// <summary>Hides the Text property so that the label text can only be changed via the Value property.</summary>
        [Browsable(false)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
        }

        /// <summary>Gets or sets the megabyte value for the label.</summary>
        public int Value
        {
            get
            {
                return this.value;
            }

            set
            {
                this.value = value;
                if (!this.reserveMode && this.value == int.MaxValue)
                {
                    base.Text = "∞ MB";
                }
                else
                {
                    string valueText = value.ToString("N0") + " MB";
                    base.Text = this.CheckReserveMode(valueText);
                }
            }
        }

        /// <summary>Checks if reserve mode is enabled and formats the label appropriately.</summary>
        /// <param name="baseText">A System.String representing the label text.</param>
        /// <returns>A System.String representing the formatted text.</returns>
        private string CheckReserveMode(string baseText)
        {
            if (!this.reserveMode)
            {
                this.ForeColor = this.defaultColor;
                return baseText;
            }

            if (this.value < 0)
            {
                this.ForeColor = Color.Red;
                return baseText;
            }
            else
            {
                this.ForeColor = Color.DarkGreen;
                return "+" + baseText;
            }
        }
    }
}
